<?php
$ops = array('display','output','import');
$op_names = array('发货列表','导出订单','导入订单');
foreach($ops as$key=>$value){
	permissions('do', 'ac', 'op', 'order', 'import', $ops[$key], '订单', '批量发货', $op_names[$key]);
}
$op = in_array($op, $ops) ? $op : 'display';
load() -> func('tpl');
$uniacid= $_W['uniacid'];
$merchants = pdo_fetchall("SELECT * FROM " . tablename('tg_merchant') . " WHERE uniacid = '{$_W['uniacid']}'  ORDER BY id DESC");
$allgoods = pdo_fetchall("select gname,id from".tablename('tg_goods')."where uniacid=:uniacid",array(':uniacid'=>$_W['uniacid']));

if ($op == 'display') {
	
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
	$condition = "  uniacid = :weid";
	$paras = array(':weid' => $_W['uniacid']);

	$status = $_GPC['status'];
	$keyword = $_GPC['keyword'];
	//商品ID
	$member = $_GPC['member'];
	//电话、姓名
	$time = $_GPC['time'];
	//下单时间

	if (empty($starttime) || empty($endtime)) {
		$starttime = strtotime('-1 month');
		$endtime = time();		
	}
	if (!empty($_GPC['time'])) {
		$starttime = strtotime($_GPC['time']['start']);
		$endtime = strtotime($_GPC['time']['end']);
		$condition .= " AND  createtime >= :starttime AND  createtime <= :endtime ";
		$paras[':starttime'] = $starttime;
		$paras[':endtime'] = $endtime;
	}
	if (!empty($_GPC['keyword'])) {
		$condition .= " AND  orderno like '%{$_GPC['keyword']}%'";
	}
	if(trim($_GPC['goodsid'])!=''){
			$condition .= " and g_id like '%{$_GPC['goodsid']}%' ";
		}
	if(trim($_GPC['goodsid2'])!=''){
		$condition .= " and g_id like '%{$_GPC['goodsid2']}%' ";
	}
	if(trim($_GPC['address'])!=''){
		
		$condition .= " and address like '%{$_GPC['address']}%' ";
	}
	
	
		if (!empty($_GPC['addresstype'])) {
		$condition .= " AND  addresstype={$_GPC['addresstype']} ";
	}
	if (!empty($_GPC['merchantid'])) {
		$condition .= " AND  merchantid={$_GPC['merchantid']} ";
	}
	if (!empty($_GPC['nickname'])) {
		$condition .= " AND openid in(select * from ".tablename(tg_member)." where nickname LIKE '%{$_GPC['nickname']}%' and  uniacid='{$_W['uniacid']}')";
	}
	if (!empty($_GPC['member'])) {
		$condition .= " AND (addname LIKE '%{$_GPC['member']}%' or mobile LIKE '%{$_GPC['member']}%')";
	}
	if ($status!='') {
		if ($status == 1) {
			$condition .= " AND is_tuan=1 ";
		} else {
			$condition .= " AND is_tuan=0 ";
		}

	}
	$godluck = $_GPC['godluck'];
	if(!empty($godluck))
	{
		$condition .= " AND godluck={$_GPC['godluck']} ";
		$condition .= " AND status in (6,8) and expresssn is NULL";
	}
	if(empty($godluck))
	{
		$condition .= " AND status in (6,8) and expresssn is NULL  and dispatchtype<>3";
	}
	
	
	$sql = "select  * from " . tablename('tg_order') . " where $condition  and mobile<>'虚拟'  ORDER BY createtime DESC " . "LIMIT " . ($pindex - 1) * $psize . ',' . $psize;
	$list = pdo_fetchall($sql, $paras);

	//echo "<pre>";print_r($list);exit;
	$paytype = array('0' => array('css' => 'default', 'name' => '未支付'), '1' => array('css' => 'info', 'name' => '余额支付'), '2' => array('css' => 'success', 'name' => '在线支付'), '3' => array('css' => 'warning', 'name' => '货到付款'));
	$orderstatus = array(
	'0' => array('css' => 'default', 'name' => '待付款'), 
	'1' => array('css' => 'info', 'name' => '已付款'),
	'2' => array('css' => 'warning', 'name' => '待收货'),
	'3' => array('css' => 'success', 'name' => '已完成'), 
	'4' => array('css' => 'success', 'name' => '已退款'),
	'5' => array('css' => 'success', 'name' => '强退款'),
	'6' => array('css' => 'danger', 'name' => '部分退款'),
	'7' => array('css' => 'success', 'name' => '已退款'),
	'8' => array('css' => 'success', 'name' => '待使用'),
	'9' => array('css' => 'success', 'name' => '已取消'),
	'10' => array('css' => 'success', 'name' => '待退款')
	);
	foreach ($list as &$value) {
		$s = $value['status'];
		$value['statuscss'] = $orderstatus[$value['status']]['css'];
		$value['status'] = $orderstatus[$value['status']]['name'];
		$value['css'] = $paytype[$value['pay_type']]['css'];
		if ($value['pay_type'] == 2) {
			if (empty($value['transid'])) {
				$value['paytype'] = '微信支付';
			} else {
				$value['paytype'] = '微信支付';
			}
		} else {
			$value['paytype'] = $paytype[$value['pay_type']]['name'];
		}
		$goodsss = pdo_fetch("select * from" . tablename('tg_goods') . "where id = '{$value['g_id']}'");
		$value['gid'] = $goodsss['id'];
		$value['gname'] = $goodsss['gname'];
		$value['gimg'] = $goodsss['gimg'];
		$value['freight'] = $goodsss['freight'];
		//$options  = pdo_fetch("select title,productprice,marketprice from " . tablename("tg_goods_option") . " where id=:id limit 1", array(":id" => $value['optionid']));
		//$value['optionname'] = $options['title'];
		$value['merchant'] = pdo_fetch("select name from" . tablename('tg_merchant') . "where id = '{$goodsss['merchantid']}' and uniacid={$_W['uniacid']}");
	}
	$total_choujian = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_order') . " WHERE uniacid='{$_W['uniacid']}' and status in (6,8) and expresssn is NULL  and godluck=1 and mobile<>'虚拟' ", $paras);
	
	$total_tuan = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_order') . " WHERE uniacid='{$_W['uniacid']}' and status in (6,8) and expresssn is NULL and is_tuan=1 and mobile<>'虚拟' and dispatchtype<>3", $paras);
	$total_single = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_order') . " WHERE uniacid='{$_W['uniacid']}' and status in (6,8) and expresssn is NULL and is_tuan=0 and mobile<>'虚拟' and dispatchtype<>3", $paras);
	$total_all = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_order') . " WHERE uniacid='{$_W['uniacid']}' and status in (6,8) and expresssn is NULL and mobile<>'虚拟' and dispatchtype<>3");
	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_order') . " WHERE $condition and mobile<>'虚拟' and dispatchtype<>3", $paras);
	$pager = pagination($total, $pindex, $psize);

} elseif ($op == 'import') {
	$file = $_FILES['fileName'];
	$max_size = "2000000";
	$fname = $file['name'];
	$ftype = strtolower(substr(strrchr($fname, '.'), 1));
	//文件格式
	$uploadfile = $file['tmp_name'];
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		if (is_uploaded_file($uploadfile)) {
			if ($file['size'] > $max_size) {
				echo "Import file is too large";
				exit ;
			}
			if ($ftype == 'xls') {
				require_once '../framework/library/phpexcel/PHPExcel.php';
				$objReader = PHPExcel_IOFactory::createReader('Excel5');
				$objPHPExcel = $objReader -> load($uploadfile);
				$sheet = $objPHPExcel -> getSheet(0);
				$highestRow = $sheet -> getHighestRow();
				$succ_result = 0;
				$error_result = 0;
				for ($j = 2; $j <= $highestRow; $j++) {
					$orderNo = trim($objPHPExcel -> getActiveSheet() -> getCell("A$j") -> getValue());
					$expressOrder = trim($objPHPExcel -> getActiveSheet() -> getCell("U$j") -> getValue());
					$expressName = trim($objPHPExcel -> getActiveSheet() -> getCell("V$j") -> getValue());
					if (!empty($expressOrder) && !empty($expressName)) {
						$order = pdo_fetch("select * from" . tablename('tg_order') . "where orderno ='{$orderNo}'");
						
						$res = pdo_update('tg_order', array('status' => 2, 'sendepressmessage'=>1, 'express' => $expressName, 'expresssn' => $expressOrder,'sendtime'=>TIMESTAMP), array('orderno' => $orderNo,'status'=>8));
						if ($res) {
							/*更新可结算金额*/
							if(!empty($order['merchantid'])){merchant_update_no_money($order['price'], $order['merchantid']);}
							/*记录操作*/
							$oplogdata = serialize($item);
							oplog('admin', "后台批量发货", web_url('order/import/import'), $oplogdata);
							$url = app_url('order/order/detail', array('id' => $order['id']));
							//send_success($order['orderno'], $order['openid'], $expressName, $orderNo, $url);
							$succ_result += 1;
						} else {
							$error_result += 1;
						}
					} else {
						if (!empty($orderNo)) {
							$error_result += 1;
						}
					}
				}
			}elseif ($ftype == 'csv') {
			    if (empty ($uploadfile)) { 
			        echo '请选择要导入的CSV文件！'; 
			        exit; 
			    } 
			    $handle = fopen($uploadfile, 'r'); 
				$n = 0; 
			    while ($data = fgetcsv($handle, 10000)) { 
			        $num = count($data); 
			        for ($i = 0; $i < $num; $i++) { 
			            $out[$n][$i] = $data[$i]; 
			        } 
			        $n++; 
			    } 
			    $result = $out; //解析csv 
			    $len_result = count($result); 
			    if($len_result==0){ 
			        echo '没有任何数据！'; 
			        exit; 
			    } 
				$succ_result = 0;
				$error_result = 0;
			    for ($i = 1; $i < $len_result; $i++) { //循环获取各字段值 
			        $orderNo = trim(iconv('gb2312', 'utf-8', $result[$i][0])); //中文转码 
			        if($orderNo==''){			
			        	continue;
			        }
			      
			        $expressOrder = trim(iconv('gb2312', 'utf-8', $result[$i][20])); 
			        $expressName =trim(iconv('gb2312', 'utf-8', $result[$i][21]));
				
					if (!empty($expressOrder) && !empty($expressName)) {
						$order = pdo_fetch("select * from" . tablename('tg_order') . "where orderno ='{$orderNo}'");
						
						$res = pdo_update('tg_order', array('status' => 2, 'sendepressmessage'=>1,'express' => $expressName, 'expresssn' => $expressOrder,'sendtime'=>TIMESTAMP), array('orderno' => $orderNo));
						if ($res) {
							$url = app_url('order/order/detail', array('id' => $order['id']));
							//send_success($order['orderno'], $order['openid'], $expressName, $orderNo, $url);
							$succ_result += 1;
						} else {
							$error_result += 1;
						}
					}else{
							$error_result += 1;
					}
			    } 
			    fclose($handle); //关闭指针 
			}else{
				echo "文件后缀格式必须为xls或csv";
				exit ;
			}
		} else {
			echo "文件名不能为空!";
			exit ;
		}
	}
	message('导入发货订单操作成功！成功' . $succ_result . '条，失败' . $error_result . '条', referer(), 'success');
} elseif ($op == 'output') {
	wl_load()->model('member');
	$status = $_GPC['status'];
	
	$keyword = $_GPC['keyword'];
	$member = $_GPC['member'];
	$time = $_GPC['time'];

	$starttime = $_GPC['starttime'];
	$endtime = $_GPC['endtime'];
	$condition = " uniacid={$_W['uniacid']}";
	if ($status!='') {
		if ($status == 1) {
			$condition .= " AND is_tuan=1 ";
		} else {
			$condition .= " AND is_tuan=0 ";
		}
	}
	$godluck = $_GPC['godluck'];
	
	if(!empty($godluck))
	{
		$condition .= " AND godluck={$_GPC['godluck']} ";
		$condition .= " AND status in (6,8)  ";
	}
	if(empty($godluck))
	{
		$condition .= " AND status in (6,8) and dispatchtype<>3";
	}
	if (!empty($_GPC['keyword'])) {
		$condition .= " AND  orderno like '%{$_GPC['keyword']}%'";
	}
	if(trim($_GPC['goodsid'])!=''){
			$condition .= " and g_id like '%{$_GPC['goodsid']}%' ";
		}
	if(trim($_GPC['goodsid2'])!=''){
		$condition .= " and g_id like '%{$_GPC['goodsid2']}%' ";
	}
	if (!empty($_GPC['merchantid'])) {
		$condition .= " AND  merchantid={$_GPC['merchantid']} ";
	}
	if ($keyword != '') {
		$condition .= " AND g_id = '{$keyword}'";
	}
	if(trim($_GPC['address'])!=''){
		
		$condition .= " and address like '%{$_GPC['address']}%' ";
	}
	if (!empty($_GPC['addresstype'])) {
		$condition .= " AND  addresstype={$_GPC['addresstype']} ";
	}
	if (!empty($member)) {
		$condition .= " AND (addname LIKE '%{$member}%' or mobile LIKE '%{$member}%')";
	}
	if (!empty($time)) {
		$condition .= " AND  createtime >= $starttime AND  createtime <= $endtime  ";
	}

//	$orders = pdo_fetchall("select * from" . tablename('tg_order') . "where $condition and status=2");
	$orders = pdo_fetchall("select * from" . tablename('tg_order') . "where $condition and mobile<>'虚拟' and expresssn is NULL order by tuan_id asc");
	if ($status == '0') {
		$str = '单独购买待使用订单_' . time();
	}
	if ($status == '1') {
		$str = '团购成功待使用订单_' . time();
	}
	if ($status == '') {
		$str = '全部待使用订单' . time();
	}
if ($godluck == 1) {
		$str = '中奖待使用订单' . time();
	}
	/* 输入到CSV文件 */
	$html = "\xEF\xBB\xBF";
	/* 输出表头 */
	$filter = array('aa' => '订单编号','ll'=>'团ID', 'bb' => '姓名', 'cc' => '电话','m2'=>'运费', 'dd' => '总价(元)', 'ee' => '状态', 'ff' => '下单时间', 'gg' => '商品名称','pp'=>'商品规格','oo'=>'购买数量','mm'=>'买家留言', 'h1' => '省','h2' => '市','h3' => '区','h4' => '常用详细地址','hh' => '订单收货地址','m6'=>'地址类型', 'm7'=>'商家备注','ii' => '微信订单号', 'jj' => '快递单号', 'kk' => '快递名称','m1'=>'配送方式','mz'=>'组团成功时间');
	foreach ($filter as $key => $title) {
		$html .= $title . "\t,";
	}
	$html .= "\n";
	//message($condition);
	foreach ($orders as $k => $v) {
		
			$thistatus = '待使用';
			$time = date('Y-m-d H:i:s', $v['createtime']);
			$options  = pdo_fetch("select title,productprice,marketprice from " . tablename("tg_goods_option") . " where id=:id limit 1", array(":id" => $v['optionid']));
			$mermber = member_get_by_params(" openid='{$v['openid']}' ");
			if(empty($options['title'])){
				$options['title'] = '不限';
			}
			if($v['dispatchtype']==0){$disname='无';}
			if($v['dispatchtype']==1){$disname='送货上门';}
			if($v['dispatchtype']==2){$disname='快递';}
			if($v['dispatchtype']==3){
				$disname='自提';
				
			}
			$add=pdo_fetch("select * from" . tablename('tg_address') . "where id = '{$v['addressid']}' ");
			if($v['addresstype']==1){
			$addresstype='公司';
		}else{
			$addresstype='家庭';
		}
			$gname="";
			$gopname="";
			if($v['g_id']>0)
			{
				$gname=$v['goodsname'];
				$gopname=$v['optionname'];
				$orders[$k]['aa'] = $v['orderno'];
				$orders[$k]['ll'] = $v['tuan_id'];
				
			$orders[$k]['bb'] = $v['addname'];
			$orders[$k]['cc'] = $v['mobile'];
			$orders[$k]['m2'] = $v['freight'];
			$orders[$k]['dd'] = $v['pay_price'];
			$orders[$k]['ee'] = $thistatus;
			$orders[$k]['ff'] = $time;
			$orders[$k]['gg'] = $gname;
			$orders[$k]['pp'] = $gopname;
			$orders[$k]['oo'] = $v['gnum'];
			$orders[$k]['mm'] = $v['remark'];	
			$orders[$k]['h1'] = $add['province'];
			$orders[$k]['h2'] = $add['city'];
			$orders[$k]['h3'] = $add['county'];
			$orders[$k]['h4'] = trim($add['detailed_address']);
			$orders[$k]['m6'] = $addresstype;
			$orders[$k]['m7'] = $v['buyremark'];
			$orders[$k]['hh'] = $v['address'];
			$orders[$k]['ii'] = $v['transid'];
			$orders[$k]['jj'] = $v['expresssn'];
			$orders[$k]['kk'] = $v['express'];
			$orders[$k]['m1'] = $disname;
			
	
			if(!empty($v['successtime']))
			{
				$v['successtime']= date('Y-m-d H:i:s', $v['successtime']);
			}
			$orders[$k]['mz'] = $v['successtime'];		
			
			foreach ($filter as $key => $title) {
				$html .= $orders[$k][$key] . "\t,";
			}
			$html .= "\n";
			}else{
				$col=pdo_fetchall("select * from ".tablename('tg_collect')." where  orderno=:orderno",array('orderno'=>$v['orderno']));
			$orders[$k]['aa'] = $v['orderno'];
			$orders[$k]['ll'] = $v['tuan_id'];
		
			$orders[$k]['bb'] = $v['addname'];
			$orders[$k]['cc'] = $v['mobile'];
			$orders[$k]['m2'] = $v['freight'];
			$orders[$k]['dd'] = $v['price'];
			$orders[$k]['ee'] = $thistatus;
			$orders[$k]['ff'] = $time;
			$orders[$k]['gg'] = $gname;
			$orders[$k]['pp'] = $gopname;
			$orders[$k]['oo'] = $v['gnum'];
			$orders[$k]['mm'] = $v['remark'];	
			$orders[$k]['h1'] = $add['province'];
			$orders[$k]['h2'] = $add['city'];
			$orders[$k]['h3'] = $add['county'];
			$orders[$k]['h4'] = trim($add['detailed_address']);
			$orders[$k]['m6'] = $addresstype;
			$orders[$k]['m7'] = $v['buyremark'];
			$orders[$k]['hh'] = $v['address'];
			$orders[$k]['ii'] = $v['transid'];
			$orders[$k]['jj'] = $v['expresssn'];
			$orders[$k]['kk'] = $v['express'];
			$orders[$k]['m1'] = $disname;
			
			
			if(!empty($v['successtime']))
			{
				$v['successtime']= date('Y-m-d H:i:s', $v['successtime']);
			}
			$orders[$k]['mz'] = $v['successtime'];	
			
				foreach ($filter as $key => $title) {
				$html .= $orders[$k][$key] . "\t,";
			}
			$html .= "\n";
				foreach ($col as $c => $cc) {
					$gs=pdo_fetch("select gname from ".tablename('tg_goods')." where  id=:id",array('id'=>$cc['sid']));
					$gname=$gs['gname'];
					$gopname=$cc['item'];
					$orders[$k]['aa'] = '';
					$orders[$k]['ll'] = '';
					
					$orders[$k]['bb'] = '';
					$orders[$k]['cc'] = '';
					$orders[$k]['dd'] = $cc['oprice'];
					$orders[$k]['ee'] = '';
					$orders[$k]['ff'] = $time;
					$orders[$k]['gg'] = $gname;
					$orders[$k]['pp'] = $gopname;
					$orders[$k]['oo'] = $cc['num'];
					
					$orders[$k]['h1'] = '';
					$orders[$k]['h2'] = '';
					$orders[$k]['h3'] = '';
					$orders[$k]['h4'] = '';
					$orders[$k]['hh'] = '';
					$orders[$k]['ii'] = '';
					$orders[$k]['jj'] = '';
					$orders[$k]['kk'] = '';
					$orders[$k]['m1'] = '';
					$orders[$k]['m2'] = '';
					$orders[$k]['m6'] = '';
					$orders[$k]['m7'] = '';
					$orders[$k]['mz'] = '';
					$orders[$k]['mm'] = '';					
					
					foreach ($filter as $key => $title) {
				$html .= $orders[$k][$key] . "\t,";
			}
			$html .= "\n";
				}
			}
			
			
			
			
		
		
	}
	/* 输出CSV文件 */
	header("Content-type:text/csv");
	header("Content-Disposition:attachment; filename={$str}.csv");
	echo $html;
	exit();
}
include wl_template('order/import');exit();
?>